package com.zkingsoft.actions.admin;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zkingsoft.model.CostCount;
import com.zkingsoft.model.PrizeCount;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.plugin.util.ExcelUtil;
import com.zkingsoft.plugin.util.ExcelUtil.ViewExcel;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.CostCountService;

/**
 * 统计管理--费用表
 * @author 老铁
 *
 */
@Controller
@RequestMapping(value = "admin/costCount")
public class CostCountController {
	 	@Resource
	 	CostCountService costCountService;
	 	
	 	
	 	/**
	 	 * 列表显示
	 	 * @param costCount
	 	 * @param pageVo
	 	 * @return
	 	 */
	 	@RequestMapping(value = "/inPage")
		public @ResponseBody AjaxResult findInPage(CostCount costCount,PaginationVO pageVo){
			if (pageVo == null) {
				pageVo = new PaginationVO();
			}
			List<CostCount> list =costCountService.findInPage(costCount, pageVo);
			
			AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK,null,null,list,costCountService.findTotal(costCount));
			return result;
			
		}
	 	
		
	 	/**
	 	 * 根据对象显示
	 	 * @param costCount
	 	 * @param pageVo
	 	 * @return
	 	 */
	 	@RequestMapping(value = "/model")
		public @ResponseBody AjaxResult findInModel(CostCount costCount){
			List<CostCount> list =costCountService.findModel(costCount);
			AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK,null,null,list,costCountService.findTotal(costCount));
			return result;
			
		}
	 	
	 	
	 	/**
	 	 * 
	 	 * @Description:导出excel
	 	 * @author:
	 	 * @return
	 	 * 返回类型 ModelAndView
	 	 * @date 2017年5月11日
	 	 */
	 	@RequestMapping(value = "/exportExcel")
	 	public @ResponseBody ModelAndView report(ModelMap model, HttpServletRequest request, HttpServletResponse response,
	 			CostCount costCount) {
	 	    try {
	 	        if (costCount.getClaName() != null && !(costCount.getClaName().equals(""))) {
	 	        	costCount.setClaName(java.net.URLDecoder.decode(costCount.getClaName(), "UTF-8"));
	 	        }
	 	    } catch (UnsupportedEncodingException e1) {
	 	        e1.printStackTrace();
	 	    }
	 	    // 查询符合条件的数据
	 	    List<CostCount> dataList = costCountService.findModel(costCount);
	 	    //把数据组装成我们要的数据，外面的list表示一行数据，也就是一个对象的信息，对应到excel的一行数据。里面的list表示对象的一个属性，对应excel的单元格
	 	    SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_DD);
	 	    List<List< Object>> list = new ArrayList<>();
	 	        for (CostCount obj : dataList) {
	 	            List<Object> temp = new ArrayList<>();
	 	            temp.add(obj.getCtSchool());
	 	            temp.add(obj.getClaName());
	 	            temp.add(obj.getCtItems());
	 	            temp.add(obj.getCtMoney());
	 	            temp.add(obj.getCtPayee());
	 	            temp.add(obj.getCtDept());
	 	            temp.add(obj.getCtRemark());
	 	            list.add(temp);
	 	        }
	 	         
	 	        //调用我们的excel工具类，导出数据
	 	    HSSFWorkbook workbook = ExcelUtil.generateExcel("统计费用列表",
	 	            new String[] { "来自校院","班级","费用名目","费用金额","领款人","领款部门","备注",}, list);
	 	    try {
	 	        ViewExcel.buildExcel(null, workbook, request, response, "统计费用列表.xls");
	 	    } catch (Exception e) {
	 	        e.printStackTrace();
	 	    }
	 	    return new ModelAndView(new ViewExcel(), model);
	 	}
}
